package model;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;

/**
 * This abstract class should be used as base class for all the model
 * elements that you will use for data binding.  This is adapted from
 * the google data binding demo.
 * 
 * @author lobas_av (google demo)
 * 
 */
public abstract class AbstractModelObject implements Serializable {
	/**
	 * This provides the actual notification support for the properties that we 
	 * will bind.  This is a utility class provided by the JDK.
	 */
	private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
			this);

	/**
	 * Adds a new property change listener to our class
	 * @param listener the listener to add
	 */
	public void addPropertyChangeListener(PropertyChangeListener listener) {
		propertyChangeSupport.addPropertyChangeListener(listener);
	}

	/**
	 * Register a change listener for a particular property.
	 * 
	 * @param propertyName the name of the property you are interested in
	 * @param listener the listener to add for this property.
	 */
	public void addPropertyChangeListener(String propertyName,
			PropertyChangeListener listener) {
		propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
	}

	/**
	 * Remove a property change listener
	 * @param listener the listener to remove
	 */
	public void removePropertyChangeListener(PropertyChangeListener listener) {
		propertyChangeSupport.removePropertyChangeListener(listener);
	}

	/**
	 * Remove a property change listener for a particular property
	 * @param propertyName the property we are no longer interested in
	 * @param listener the listener
	 */
	public void removePropertyChangeListener(String propertyName,
			PropertyChangeListener listener) {
		propertyChangeSupport.removePropertyChangeListener(propertyName,
				listener);
	}

	/**
	 * Notify all the listeners that a property has changed
	 * @param propertyName the name of the property that changed
	 * @param oldValue the value before the change
	 * @param newValue the value after the change
	 */
	protected void firePropertyChange(String propertyName, Object oldValue,
			Object newValue) {
		propertyChangeSupport.firePropertyChange(propertyName, oldValue,
				newValue);
	}
}